package com.itheima.health.config;

import com.itheima.health.security.SecurityUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private SecurityUserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("http://localhost:8080/pages/ordersetting.html")
                .access("hasAuthority('ORDERSETTING')");
        http.authorizeRequests()
                .antMatchers("http://localhost:8080/pages/report_business.html",
                        "http://localhost:8080/pages/report_member.html",
                        "http://localhost:8080/pages/report_setmeal.html")
                .access("hasAuthority('REPORT_VIEW')");
        http.csrf().disable();
        http.formLogin()
                .loginPage("http://localhost:8080/pages/login.html")
                .loginProcessingUrl("/sec/login")
                .successForwardUrl("/user/loginSuccess")
                .failureForwardUrl("/user/loginFail");
        http.logout()
                .logoutUrl("/sec/logout")
                .logoutSuccessUrl("http://localhost:8080/pages/login.html");
    }
}
